//
//  headimage.m
//  CivilianTravel
//
//  Created by xalo on 16/7/16.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "headimage.h"
#import "SetViewController.h"

@interface headimage ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) UIImagePickerController *imagePicker;

@end

@implementation headimage

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.image);
    self.imageview =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.imageview.image = self.image;
    [self.view addSubview:self.imageview];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"更换头像" style:UIBarButtonItemStylePlain target:self action:@selector(replaceimage:)];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(replaceimage1:)];
    
     self.imagePicker = [[UIImagePickerController alloc]init];
    // Do any additional setup after loading the view.
}
-(void)replaceimage1:(id)sender{
    if (self.block) {
        self.block(self.imageview.image);
    }

    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 


-(void)replaceimage:(id)sender{
    //底部提示框
    UIAlertController *  alert = [UIAlertController alertControllerWithTitle:@"" message:@"系统消息" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //添加取消按钮
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:actionCancel];
    //添加按钮
    UIAlertAction *pickPhoto = [UIAlertAction actionWithTitle:@"调用相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            _imagePicker.delegate = self;
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
    [alert addAction:pickPhoto];
    
    //添加按钮
    UIAlertAction *takePhoto = [UIAlertAction actionWithTitle:@"调用相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            _imagePicker.delegate = self;
            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
 
 
            
            [self presentViewController:_imagePicker animated:YES completion:nil];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请使用真机调用相机" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                [self performSelector:@selector(removeAlertView:) withObject:alert afterDelay:0.8];
            }];
        }
    }];
    [alert addAction:takePhoto];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
}
- (void)removeAlertView:(UIAlertController*)alert
{
    //alertView消失
    [alert dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *sourceImage = info[UIImagePickerControllerOriginalImage];
    self.imageview.image = sourceImage;
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
